package cn.rcth.base.serivce.impl;

import cn.rcth.base.mapper.UserProductCountMapper;
import cn.rcth.base.serivce.UserProductCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: yanghuanyu
 * @Date: 2019/5/25 14:17
 * @Version 1.0
 */
@Service
public class UserProductCountServiceImpl implements UserProductCountService {

    @Autowired
    private UserProductCountMapper userProductCountMapper;

    @Override
    public Long getUserProductCount(Long userId,Long productId) {
        Long count = userProductCountMapper.getUserProductCount(userId,productId);
        return count;
    }

    @Override
    public Boolean updateUserProductCount(Long userId, Long productId,Integer buyCount) {
        Long count = userProductCountMapper.getUserProductCount(userId,productId);
        Integer insert;
        if(count == null || count <= 0){
            /**
             * 插入数据
             */
             insert = userProductCountMapper.insert(userId, productId,Long.valueOf(buyCount));
        }else {
            /**
             * 更新数据库 +1
             */
            insert = userProductCountMapper.updateCountByUserIdAndProductId(userId, productId,Long.valueOf(buyCount+count));
        }
        return insert == null || insert <= 0 ? false:true;
    }


}
